package sjtu.ist.warehouse4ws.tool.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.service.FileInfoService;
import sjtu.ist.warehouse4ws.tool.BaseAction;
import sjtu.ist.warehouse4ws.tool.ContextSession;
import sjtu.ist.warehouse4ws.tool.MyUtils;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;

public class FileAction extends BaseAction {
	private Map<String, Object> infos = new HashMap<String, Object>();
	private FileInfoService fileInfoService;
	public static final String ROOT = "upload\\";

	private File myUpload;

	private String myUploadContentType;

	private String myUploadFileName;

	private String path;

	private boolean success;

	private String id;
	private int type;

	/**
	 * 上传文件
	 * 
	 * @return
	 */
	public String uploadFiles() {
		String rootPath = getSession().getServletContext().getRealPath("/");

		rootPath += ROOT;
		String sp = rootPath + getPath();
		System.out.println(sp);
		MyUtils.mkDirectory(sp);
		try {
//			ActionContext actionContext = ActionContext.getContext();
//			Map session = actionContext.getSession();
//			Object obj=session.get("user_id");
//			String user_id = session.get("user_id").toString();
//			String user_company = session.get("user_company").toString();

			String uploadFileName = getMyUploadFileName();
			// List<Fileinfo>
			// list=fileInfoService.findFileInfoByName(uploadFileName);
			// 把文件名和文件ID写进FileInfo
			Fileinfo fileInfo = new Fileinfo();
			fileInfo.setId(java.util.UUID.randomUUID().toString());
			fileInfo.setName(uploadFileName);// 临时文件名
			fileInfo.setXmlpath(sp + fileInfo.getId());
//			fileInfo.setCompany(user_company);
			fileInfo.setType(0);
			// if (list.toString()=="[]"){
			MyUtils.upload(fileInfo.getId(), sp, getMyUpload());
			id = fileInfo.getId();
			
			type=parseFileType(fileInfo.getXmlpath());
			if(type==0){
				
			}else{
				fileInfoService.saveFileInfo(fileInfo);
			}
			this.success = true;

		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	//解析文件类型
	public int parseFileType(String filePath) throws IOException {
		int type = 0;
		FileReader fr=new FileReader(filePath);
		BufferedReader reader = new BufferedReader(fr);
		if (reader.ready()) {
			String line = reader.readLine();
			if ((line.subSequence(1, line.length() - 1)).toString().equals(
					"business_process")) {
				type = 1;//"流程模型";
			} else if ((line.subSequence(1, line.length() - 1)).toString()
					.equals("function_point")) {
				type = 2;//"功能模型";
			} else if ((line.subSequence(1, line.length() - 1)).toString()
					.equals("permission")) {
				type = 6;//"权限模型";
			} else if ((line.subSequence(1, line.length() - 1)).toString()
					.equals("domain_model")) {
				type = 4;//"业务对象模型";
			} else if ((line.subSequence(1, line.length() - 1)).toString()
					.equals("rols")) {
				type = 7;//"角色模型";

			}else if (line.contains("epml")) {
				type = 5;//"EPC模型";

			} else
				type = 0;//"无模型";
		}
		reader.close();
		fr.close();
		return type;
	}

	public File getMyUpload() {
		return myUpload;
	}

	public void setMyUpload(File myUpload) {
		this.myUpload = myUpload;
	}

	public String getMyUploadContentType() {
		return myUploadContentType;
	}

	public void setMyUploadContentType(String myUploadContentType) {
		this.myUploadContentType = myUploadContentType;
	}

	public String getMyUploadFileName() {
		return myUploadFileName;
	}

	public void setMyUploadFileName(String myUploadFileName) {
		this.myUploadFileName = myUploadFileName;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) throws UnsupportedEncodingException {
		this.path = URLDecoder.decode(path, "UTF-8");
	}

	public Map<String, Object> getInfos() {
		return infos;
	}

	public void setInfos(Map<String, Object> infos) {
		this.infos = infos;
	}

	public String getId() {
		return id;
	}

	@JSON(name = "id")
	public void setId(String id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	@JSON(name = "type")
	public void setType(int type) {
		this.type = type;
	}

	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}
}
